/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 06-Dec-2009, 21:44:38
 */
package edu.iyte.ceng431.hw03.cdplayer.view;

import edu.iyte.ceng431.hw03.cdplayer.model.Drawer;
import edu.iyte.ceng431.hw03.cdplayer.model.PlayingCD;
import edu.iyte.ceng431.hw03.cdplayer.model.Track;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import javax.swing.event.ChangeListener;

/**
 *
 * @author CUNEYT
 */
public class MainFrame extends javax.swing.JFrame implements Observer {

    private LoadCDPanel loadPanel;

    /** Creates new form MainFrame */
    public MainFrame() {

        
        initComponents();
        loadPanel = new LoadCDPanel(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        playPauseButton = new javax.swing.JButton();
        loadEjectButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        displayArea = new javax.swing.JTextArea();
        stopButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        volumeLabel = new javax.swing.JLabel();
        volumeSlider = new javax.swing.JSlider();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("E-GO PLAYER");
        setResizable(false);

        playPauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/play.png"))); // NOI18N

        loadEjectButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/eject.png"))); // NOI18N

        displayArea.setBackground(new java.awt.Color(204, 204, 204));
        displayArea.setColumns(20);
        displayArea.setEditable(false);
        displayArea.setForeground(new java.awt.Color(102, 102, 102));
        displayArea.setRows(5);
        displayArea.setText("No CD\nStopped");
        jScrollPane1.setViewportView(displayArea);

        stopButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/stop.png"))); // NOI18N

        previousButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/previous.png"))); // NOI18N

        nextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/next.png"))); // NOI18N

        volumeLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/volume.png"))); // NOI18N
        volumeLabel.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        volumeLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        volumeLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        volumeLabel.setText(String.valueOf(volumeSlider.getValue()));

        volumeSlider.setOrientation(javax.swing.JSlider.VERTICAL);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(playPauseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(loadEjectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(volumeLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(volumeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(stopButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(loadEjectButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(nextButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(playPauseButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(previousButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(volumeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(volumeSlider, 0, 0, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea displayArea;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton loadEjectButton;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton playPauseButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JButton stopButton;
    private javax.swing.JLabel volumeLabel;
    private javax.swing.JSlider volumeSlider;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the displayArea
     */
    public javax.swing.JTextArea getDisplayArea() {
        return displayArea;
    }

    /**
     * @param displayArea the displayArea to set
     */
    public void setDisplayArea(javax.swing.JTextArea displayArea) {
        this.displayArea = displayArea;
    }

    /**
     * @return the loadEjectButton
     */
    public javax.swing.JButton getLoadEjectButton() {
        return loadEjectButton;
    }

    /**
     * @param loadEjectButton the loadEjectButton to set
     */
    public void setLoadEjectButton(javax.swing.JButton loadEjectButton) {
        this.loadEjectButton = loadEjectButton;
    }

    /**
     * @return the playPauseButton
     */
    public javax.swing.JButton getPlayPauseButton() {
        return playPauseButton;
    }

    /**
     * @param playPauseButton the playPauseButton to set
     */
    public void setPlayPauseButton(javax.swing.JButton playPauseButton) {
        this.playPauseButton = playPauseButton;
    }

    /**
     * @return the previousButton
     */
    public javax.swing.JButton getPreviousButton() {
        return previousButton;
    }

    /**
     * @param previousButton the previousButton to set
     */
    public void setPreviousButton(javax.swing.JButton previousButton) {
        this.previousButton = previousButton;
    }

    /**
     * @return the nextButton
     */
    public javax.swing.JButton getNextButton() {
        return nextButton;
    }

    /**
     * @param nextButton the nextButton to set
     */
    public void setNextButton(javax.swing.JButton nextButton) {
        this.nextButton = nextButton;
    }

    /**
     * @return the stopButton
     */
    public javax.swing.JButton getStopButton() {
        return stopButton;
    }

    /**
     * @param stopButton the stopButton to set
     */
    public void setStopButton(javax.swing.JButton stopButton) {
        this.stopButton = stopButton;
    }

    /**
     * @return the volumeSlider
     */
    public javax.swing.JSlider getVolumeSlider() {
        return volumeSlider;
    }

    /**
     * @param volumeSlider the volumeSlider to set
     */
    public void setVolumeSlider(javax.swing.JSlider volumeSlider) {
        this.volumeSlider = volumeSlider;
    }

    public void addPreviousButtonActionPerformed(ActionListener actionListener) {
        previousButton.addActionListener(actionListener);
    }

    public void addPlayButtonActionPerformed(ActionListener actionListener) {
        playPauseButton.addActionListener(actionListener);
    }

    public void addNextButtonActionPerformed(ActionListener actionListener) {
        nextButton.addActionListener(actionListener);
    }

    public void addStopButtonActionPerformed(ActionListener actionListener) {
        stopButton.addActionListener(actionListener);
    }

    public void addEjectButtonActionPerformed(ActionListener actionListener) {
        loadEjectButton.addActionListener(actionListener);
    }

    public void addVolumeSliderChanged(ChangeListener changeListener) {
        volumeSlider.addChangeListener(changeListener);
    }

    public void addLoadButtonActionPerformed(ActionListener actionListener) {
        loadPanel.addLoadButtonActionPerformed(actionListener);
    }

    /*
     * Updates the information displayed in the screen
     * The info displayed includes information as follows:
     *      CD Title
     *      Total CD length
     *      Current Track and length
     *      State of the CD player (playing,paused,stopped)
     */
    public void update(Observable o, Object arg) {
        if (o instanceof Drawer) {
            loadPanel.setVisible((Boolean) arg);
        } else if (o instanceof PlayingCD) {
            PlayingCD cd = (PlayingCD) o;
            String info = "CD Title : " + cd.getTitle() +" Total Length: "+ cd.calculateTotalLength();
            Track currentTrack = cd.getCurrentTrack();
            if (currentTrack != null) {
                info += "\nCurrent Track : " + currentTrack.getTitle() + " - " + currentTrack.getArtist() + " - " +
                        currentTrack.getAlbum() + " " + currentTrack.getFormattedLength() + "\n";
                if (cd.isPlaying()) {
                    info += "Playing";
                    playPauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/pause.png")));
                } else {
                    info += "Paused";
                    playPauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/play.png"))); // NOI18N
                }

            } else {
                info += "\nStopped";
                playPauseButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/iyte/ceng431/hw03/cdplayer/view/images/play.png"))); // NOI18N
            }
            displayArea.setText(info);
        }
    }

    /**
     * @return the volumeLabel
     */
    public javax.swing.JLabel getVolumeLabel() {
        return volumeLabel;
    }

    /**
     * @param volumeLabel the volumeLabel to set
     */
    public void setVolumeLabel(javax.swing.JLabel volumeLabel) {
        this.volumeLabel = volumeLabel;
    }
}
